package com.ssish.eoms.back.accounting.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.ssish.eoms.back.report.domain.LJYSPolciytListDO;
import com.ssish.eoms.common.contants.Urls;
import com.ssish.eoms.common.dto.CustomerDTO;
import com.ssish.eoms.common.dto.CustomerWebUserDTO;
import com.ssish.eoms.util.enums.RechargeTypeEnums;
import com.ssish.eoms.util.ftp.FtpUtil;

import net.sf.json.JSONObject;

public class FtpUtilTest {
	
	
	
	public void writeDateToWeb(HttpServletResponse respones,JSONObject jb) throws IOException{
		PrintWriter out = null;
		try {
			respones.setCharacterEncoding("UTF-8");
			respones.setHeader("Content-Type", "text/html; charset=utf-8");
			out = respones.getWriter();
			out.println(jb.toString());
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		}finally {
			if (out != null) {
				out.close();
			}
		}
	}
	
	
	//上传
	@RequestMapping(value = Urls.ACCOUNTING_RECHAGE_UPLOAD, method = RequestMethod.POST)
	public void upload(@RequestParam(value = "uploadify", required = false) MultipartFile uploadify,String voucherNo ,String tzs,final String addAmount,String id,String amount,String loginNa,String customerNa , HttpServletRequest request,
			HttpServletResponse respones, ModelMap model) throws Exception{
		JSONObject jb = new JSONObject();
		Map mapRe= new HashMap<String, String>();
		boolean falg = false;
		FtpUtil f = new FtpUtil("114.55.39.153", 21, "ftpadmin", "ssinanyan2016");
		// 本地文件目录和文件名
		// String localDirectoryAndFileName= uploadify.getOriginalFilename();
		String localFileName = uploadify.getOriginalFilename();
		// 上传后的文件名
		// String ftpFileName="testt";
		String ftpFileName ="";
		String suffix = ".pdf";
		try{
			suffix = localFileName.substring(localFileName.lastIndexOf("."),localFileName.length());
		}catch(Exception e){
			System.out.println("eoms上传文件后缀名处理错误");
		}
		LJYSPolciytListDO ljysPolciytListDO=new LJYSPolciytListDO();
		String ftpDirectory = "../ftpadmin/rechage/"+tzs+"/";
		ftpFileName =new Date().getTime()+suffix;
		//插入充值记录
				Map map= new HashMap<String, Object>();
				map.put("id", id);
				CustomerWebUserDTO customerWebUserDTO=null;
				CustomerDTO customerDTO=null;
				if(StringUtils.isNotBlank(loginNa)){
					map.put("loginName", loginNa);
					customerWebUserDTO=customerWebUserService.queryCustomerWebUserDTOByacctingId(map);
				}else{
					map.put("customerName", customerNa);
					customerDTO=customerService.queryCustomerDTObyAccid(map);
				}
				
				String newId = "";
				if(customerWebUserDTO==null){
					newId = accountingService.accountingRechargeOrder(id, customerDTO.getCustomerid()+"", Double.parseDouble(addAmount), Double.parseDouble(amount), "在eoms中充值", RechargeTypeEnums.BACK,voucherNo,ftpDirectory +ftpFileName);
				}else {
					newId = accountingService.accountingRechargeOrder(id, customerWebUserDTO.getCuswebuserid()+"", Double.parseDouble(addAmount), Double.parseDouble(amount), "在eoms中充值", RechargeTypeEnums.BACK,voucherNo,ftpDirectory +ftpFileName);
				}
		try {
			if (f.open()) {
				f.put(uploadify, localFileName, ftpFileName, ftpDirectory);
				f.close();
				jb.put("Id", newId);
			}
		} catch (Exception e) {
			model.addAttribute("result", false);
			e.printStackTrace();
		}
		writeDateToWeb(respones, jb);
	}
	
	
	
	//下载
	@RequestMapping(value=Urls.ACCOUNTING_RECHAGE_DOWNLOAD_FILE,method=RequestMethod.GET)
	public void downloadFiles(String path,HttpServletResponse response) throws IOException{
		if(org.apache.commons.lang3.StringUtils.isNotBlank(path)){
			String[] strs=path.split("/");
			String realFileName=strs[strs.length-1];
			response.setContentType("application/vnd.ms-excel"); 
			response.setContentType("application/jsonrequest");
			response.setCharacterEncoding("UTF-8");
			response.setHeader("Content-type", "text/html;charset=UTF-8");
			response.setHeader("Access-Control-Allow-Origin","*");
			response.setHeader("Access-Control-Allow-Methods","POST");
			response.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type");
			response.setHeader("content-disposition", "attachment;filename=" +java.net.URLEncoder.encode(realFileName, "UTF-8"));
			FtpUtil f = new FtpUtil("114.55.39.153", 21, "ftpadmin", "ssinanyan2016");
			if (f.open()) {
				f.getFile(path, response.getOutputStream());
				f.close();
			}
		}
	}
	
}
